<template>
  <div class="image-preview" :style="{ height, width }">
    <img :src="src" :style="{ height, width }" />
    <div class="image-preview__inner">
      <Icon type="ios-eye-outline" @click.native="handleImgView(src)"></Icon>
      <Icon v-if="hasRemove" type="ios-trash-outline" @click.native="handleImgRemove"></Icon>
    </div>
    <Modal title="预览图片" v-model="previewImgVisable" class="image-preview__dialog">
      <img :src="previewImgURL" v-if="previewImgVisable" class="image-preview__img" />
      <br />
      <br />
      <p class="image-preview__url">图片地址: {{previewImgURL}}</p>
    </Modal>
  </div>
</template>

<script>
export default {
  name: 'm-image-preview',

  props: {
    src: String,
    height: { type: String, default: '60px' },
    width: { type: String, default: '100%' },
    hasRemove: Boolean
  },

  data () {
    return {
      previewImgVisable: false, // 是否打开预览
      previewImgURL: '' // 预览图片地址
    };
  },

  methods: {
    handleImgView (url) {
      this.previewImgURL = url;
      this.previewImgVisable = true;
    },
    handleImgRemove () {
      this.$emit('remove');
    }
  }
};
</script>

<style lang="scss">
.image-preview {
  position: relative;

  &:hover &__inner {
    display: flex;
  }

  &__inner {
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.6);
    flex-flow: row nowrap;
    justify-content: center;
    align-items: center;

    & > i {
      color: #fff;
      font-size: 20px;
      cursor: pointer;
      margin: 0 2px;
    }
  }

  &__dialog .ivu-modal {
    width: fit-content !important;
  }

  &__img {
    width: auto;
    max-height: 400px;
    margin: 0 auto;
    display: block;
  }

  &__url {
    word-break: break-all;
  }
}
</style>
